{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Gallery\n",
    "\n",
    "Rather than presenting a tutorial, this notebook collects a \"gallery\" of datasets that you can explore in WWT."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Setup (Run Me First!)\n",
    "\n",
    "As always, the first step is to connect to the WWT app. **If WWT isn't running yet, activate the JupyterLab command palette from the View menu, and select \"WorldWide Telescope\"**. Then run:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pywwt.jupyter import connect_to_app\n",
    "wwt = await connect_to_app().becomes_ready()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the `wwt` variable will let us talk to the WWT app. The following command will make the environment a bit more uniform:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.foreground_opacity = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## PHAT (large FITS images)\n",
    "\n",
    "These commands load and display images from the [Panchromatic Hubble Andromeda Treasury][phat], a large Hubble Space Telescope survey of the Andromeda galaxy (M31). Datasets in the F475W and F814W filters have been pre-loaded into WWT's\n",
    "data format. Each source mosaic consists of about 12 GB of FITS data.\n",
    "\n",
    "[phat]: https://archive.stsci.edu/prepds/phat/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PHAT_WTML_URL = \"http://data1.wwtassets.org/packages/2021/09_phat_fits/index.wtml\"\n",
    "PHAT_F475W_URL = \"http://data1.wwtassets.org/packages/2021/09_phat_fits/f475w/{1}/{3}/{3}_{2}.fits\"\n",
    "PHAT_F814W_URL = \"http://data1.wwtassets.org/packages/2021/09_phat_fits/f814w/{1}/{3}/{3}_{2}.fits\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.load_image_collection(PHAT_WTML_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running the above command, you can pull up the PHAT images in the WWT GUI using the \"Add imagery as layer\" menu item. Or you can add the images programmatically with the following commands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "phat475 = wwt.layers.add_preloaded_image_layer(PHAT_F475W_URL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "phat814 = wwt.layers.add_preloaded_image_layer(PHAT_F814W_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Rubin/LSST DESC DC2 (large FITS images)\n",
    "\n",
    "These commands load and display simulated Rubin Observatory images from the [LSST DESC DC2 Simulated Sky Survey][desc]. Five overlapping DC2 fields have been pre-loaded into WWT's data format. Each image consists of about 3 GB of FITS data.\n",
    "\n",
    "[desc]: https://data.lsstdesc.org/doc/dc2_sim_sky_survey\n",
    "\n",
    "Note that the DC2 images are labeled with sky coordinates but do **not** correspond to the actual night sky, so the sources in the imagery will **not** match up with real astrophysical sources."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DESC_WTML_URL = \"https://data1.wwtassets.org/packages/2022/06_desc22i/index.wtml\"\n",
    "_desc_ids = \"00002337 00012483 00183818 00202590 00254379\".split()\n",
    "_desc_image_urls = [\"http://data1.wwtassets.org/packages/2022/06_desc22i/%s/{1}/{3}/{3}_{2}.fits\" % i for i in _desc_ids]\n",
    "_desc_cuts = [\n",
    "    (1040, 1070),\n",
    "    (4000, 4150),\n",
    "    (1420, 1490),\n",
    "    (1850, 1900),\n",
    "    (1390, 1430),\n",
    "]\n",
    "\n",
    "desc_images = [None] * len(_desc_image_urls)\n",
    "\n",
    "def load_desc(i):\n",
    "    global desc_images\n",
    "    desc_images[i] = wwt.layers.add_preloaded_image_layer(_desc_image_urls[i])\n",
    "    desc_images[i].vmin = _desc_cuts[i][0]\n",
    "    desc_images[i].vmax = _desc_cuts[i][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.load_image_collection(DESC_WTML_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running the above command, you can pull up the DC2 images in the WWT GUI using the \"Add imagery as layer\" menu item. Or you can add them programmatically using the helper function declared above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_desc(1)  # valid arguments are 0 through 4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This function automatically sets custom data cuts for each image to pull out features such as fringing and large-scale flatness issues. If you add the images in the GUI, you'll need to adjust these manually."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Hyper Suprime-Cam (large FITS image)\n",
    "\n",
    "These commands load and display a large mosaic from the [Hyper Suprime-Cam][hsc] instrument on the Subaru telescope, provided by Yusra Al-Sayyad. The total data size is about 9 GB.\n",
    "\n",
    "[hsc]: https://www.naoj.org/Projects/HSC/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HSC_WTML_URL = \"http://data1.wwtassets.org/packages/2021/08_hscfits/index.wtml\"\n",
    "HSC_IMAGE_URL = \"http://data1.wwtassets.org/packages/2021/08_hscfits/{1}/{3}/{3}_{2}.fits\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.load_image_collection(HSC_WTML_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running the above command, you can pull up the mosaic in the WWT GUI\n",
    "using the \"Add imagery as layer\" menu item. Or you can add it programmatically as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hsc = wwt.layers.add_preloaded_image_layer(HSC_IMAGE_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you adjust the view settings to bring out faint structures, you may be able to find a large-scale “waffling” pattern in the calibration. This is the kind of effect that is hard to discover without WWT's multi-resolution capabilities!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## MeerKAT Galactic Center (large FITS images)\n",
    "\n",
    "These commands load and display [a large mosaic of the Galactic Center][mkgc] from the MeerKAT radio telescope. There are two image planes: the total intensity (Stokes I) at a mean frequency of 1.3 GHz, and a spectral index image. The images are about 1 GB of FITS data each.\n",
    "\n",
    "[mkgc]: https://archive-gw-1.kat.ac.za/public/repository/10.48479/fyst-hj47/index.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MKGC_STOKESI_WTML_URL = \"http://data1.wwtassets.org/packages/2022/01_meerkat_gc/MeerKAT_Galactic_Centre_1284MHz-StokesI_tiled/index.wtml\"\n",
    "MKGC_STOKESI_IMAGE_URL = \"http://data1.wwtassets.org/packages/2022/01_meerkat_gc/MeerKAT_Galactic_Centre_1284MHz-StokesI_tiled/{1}/{3}/{3}_{2}.fits\"\n",
    "MKGC_ALPHA_WTML_URL = \"http://data1.wwtassets.org/packages/2022/01_meerkat_gc/MeerKAT_Galactic_Centre_alpha_tiled/index.wtml\"\n",
    "MKGC_ALPHA_IMAGE_URL = \"http://data1.wwtassets.org/packages/2022/01_meerkat_gc/MeerKAT_Galactic_Centre_alpha_tiled/{1}/{3}/{3}_{2}.fits\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.load_image_collection(MKGC_STOKESI_WTML_URL)\n",
    "wwt.load_image_collection(MKGC_ALPHA_WTML_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running the above command, you can pull up the mosaics in the WWT GUI using\n",
    "the \"Add imagery as layer\" menu item. Or you can add them programmatically as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mkgc_stokesi = wwt.layers.add_preloaded_image_layer(MKGC_STOKESI_IMAGE_URL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mkgc_alpha = wwt.layers.add_preloaded_image_layer(MKGC_ALPHA_IMAGE_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## LOFAR Lockman Hole (large FITS image)\n",
    "\n",
    "These commands load and display [a large mosaic of the Lockman Hole][lockman] at about 140 MHz from DR1 of the LOFAR LoTSS-Deep High Definition survey project. The mosaic consists of about 30 GB of FITS data that have been pre-loaded into WWT's formats.\n",
    "\n",
    "[lockman]: https://www.lofar-surveys.org/hdfields.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "LOCKMAN_WTML_URL = \"http://data1.wwtassets.org/packages/2022/03_LockmanHole_LoTSS-deepHD_dr1/index.wtml\"\n",
    "LOCKMAN_IMAGE_URL = \"http://data1.wwtassets.org/packages/2022/03_LockmanHole_LoTSS-deepHD_dr1/{1}/{3}/{3}_{2}.fits\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wwt.load_image_collection(LOCKMAN_WTML_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After running the above command, you can pull up the mosaic in the WWT GUI using\n",
    "the \"Add imagery as layer\" menu item. Or you can add it programmatically as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lockman = wwt.layers.add_preloaded_image_layer(LOCKMAN_IMAGE_URL)\n",
    "lockman.vmin = -0.00002\n",
    "lockman.vmax = 0.0001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you add the image manually, note that you will need to tweak the data cuts significantly in order to be able to see any detail."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## Gaia EDR3 (HiPS Catalog)\n",
    "\n",
    "These commands add the [Gaia EDR3 source catalog][edr3] to the WWT view. You can\n",
    "use the GUI to load up this catalog without needing to run any code at all (using the \"Add HiPS catalogs\" menu item), but the Python-based approach allows you to\n",
    "fetch the currently-visible catalog data as an AstroPy table.\n",
    "\n",
    "[edr3]: https://www.cosmos.esa.int/web/gaia/earlydr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "GAIA_EDR3_CAT_NAME = \"Gaia EDR3 (Gaia Collaboration, 2020) (gaiaedr3)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat = await wwt.layers.add_hips_catalog_layer(GAIA_EDR3_CAT_NAME)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following commands will fetch the catalog data contained in the current WWT view as an AstroPy table. If the view is very zoomed out, the command will take a long time (and eventually hit a timeout or row limit)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tbl = await cat.refresh()\n",
    "tbl"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
